package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.ChatRecord;
import com.zzxz.practicaltrainingsys.service.ChatManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatManageController {

    @Autowired
    private ChatManageService chatManageService;

    /**
     * 获取所有聊天记录列表
     */
    @GetMapping("/records/list")
    public ResponseEntity<List<ChatRecord>> getChatRecordsList() {
        List<ChatRecord> records = chatManageService.getAllChatRecords();
        return ResponseEntity.ok(records);
    }

    /**
     * 根据邀请码获取群聊消息
     */
    @GetMapping("/group/{inviteCode}")
    public ResponseEntity<List<ChatRecord>> getGroupMessages(@PathVariable String inviteCode) {
        List<ChatRecord> groupMessages = chatManageService.getGroupMessages(inviteCode);
        return ResponseEntity.ok(groupMessages);
    }

    /**
     * 删除单条聊天记录
     */
    @DeleteMapping("/record/{id}")
    public ResponseEntity<Integer> deleteChatRecord(@PathVariable Long id) {
        int result = chatManageService.deleteChatRecord(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 批量删除聊天记录
     */
    @DeleteMapping("/records/batch")
    public ResponseEntity<Integer> batchDeleteChatRecords(@RequestBody Map<String, List<Long>> request) {
        List<Long> ids = request.get("ids");
        int result = chatManageService.batchDeleteChatRecords(ids);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除整个群聊（根据邀请码）
     */
    @DeleteMapping("/group/{inviteCode}")
    public ResponseEntity<Integer> deleteGroupChat(@PathVariable String inviteCode) {
        int result = chatManageService.deleteGroupChat(inviteCode);
        return ResponseEntity.ok(result);
    }

    /**
     * 清理历史消息（根据天数）
     */
    @DeleteMapping("/records/clear")
    public ResponseEntity<Integer> clearOldMessages(@RequestParam int days) {
        int result = chatManageService.clearOldMessages(days);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存聊天记录（用于聊天功能）
     */
    @PostMapping("/record")
    public ResponseEntity<Integer> saveChatRecord(@RequestBody ChatRecord chatRecord) {
        int result = chatManageService.saveChatRecord(chatRecord);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据条件查询聊天记录
     */
    @GetMapping("/records/search")
    public ResponseEntity<List<ChatRecord>> searchChatRecords(
            @RequestParam(required = false) String userType,
            @RequestParam(required = false) String chatType,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String inviteCode,
            @RequestParam(required = false) String keyword) {

        List<ChatRecord> records = chatManageService.searchChatRecords(
                userType, chatType, username, inviteCode, keyword);
        return ResponseEntity.ok(records);
    }
}